/*******************************************************************************
 * Venice Noise Android Application
 * Copyright (C) 2011  Worcester Polytechnic Institute, Wesley Ripley
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * If you plan to use or modify any of this code please contact Fabio Carrera (carrera.fabio@gmail.com)
 * Also please attribute any code used in the final product to the developers. 
 * 
 * Author: Wesley Ripley (wripley@wpi.edu) 
 *******************************************************************************/
package org.venicenoise.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;

/**
 * A class used to make a dialog informing the user they need
 * to enable their location services.
 * 
 * @author Wesley Ripley
 * @version 12/7/2011
 */
public class EnableLocationDialog{
	
	/**
	 * A convenience method to create the default dialog for either just
	 * enabling the GPS or all location services
	 * @param gpsOnly
	 * 		Whether we are informing the user to enable just the GPS, or all location services.
	 * 		true means we are informing them just about the GPS and the dialog can be skipped
	 * 		false means we are informing them no location services are disabled and the dialog cannot be skipped
	 * @param onCancel
	 * 		The listener defining what should happen when the dialog is cancelled. 
	 * @param onSkipClicked
	 * 		The listener defining what should happen when the skip button is clicked.
	 * 		Ignored if gpsOnly false
	 * @param activity
	 * 		The activity to use to launch the location settings activity. This is the activity that
	 * 		onActivityResult will be called on when the user returns from the location settings
	 * @param newActivityId
	 * 		The id to be used as the requestCode when starting the location settings activity. This
	 * 		will be the value of requestCode when onActivityResult gets called on the given activity
	 * @return
	 * 		The dialog (not yet showing) created according to the given parameters
	 */
	public static Dialog makeDialog(final boolean gpsOnly, OnCancelListener onCancel, 
									DialogInterface.OnClickListener onSkipClicked,final Activity activity,
									final int newActivityId)
	{
		String settingButtonText = activity.getString(R.string.locationSettings);
		if(gpsOnly)
		{
			return makeDialog(activity.getString(R.string.noGPS),activity.getString(R.string.enableGPS),
							settingButtonText,true,onCancel,true,activity.getString(R.string.skipEnableGPS),onSkipClicked,
							activity,newActivityId);
		}
		else
		{
			return makeDialog(activity.getString(R.string.noLocationEnabled),activity.getString(R.string.enableLocation),
							settingButtonText,true,onCancel,false,null,null,activity,newActivityId);
		}
	}
	
	/**
	 * Create a completely custom enable location dialog
	 * @param title
	 * 		The title of the dialog
	 * @param message
	 * 		The message to be displayed in the dialog
	 * @param settingsButtonText
	 * 		The text of the button that will take the user to the location settings
	 * @param cancelable
	 * 		Whether the user should be able to cancel the dialog or not.
	 * @param onCancel
	 * 		The listener defining what should happen when the dialog is cancelled. 
	 * 		Ignored if cancelable is false.
	 * @param skipable
	 * 		Whether or not the dialog can be skipped. True if it can be skipped, false otherwise.
	 * @param skipButtonText
	 * 		The text of the button to let the user skip enabling location. Ignored if skipable is false
	 * @param onSkipClicked
	 * 		The listener defining what should happen when the skip button is clicked. Ignored if skipable is false
	 * @param activity
	 * 		The activity to use to launch the location settings activity. This is the activity that
	 * 		onActivityResult will be called on when the user returns from the location settings
	 * @param newActivityId
	 * 		The id to be used as the requestCode when starting the location settings activity. This
	 * 		will be the value of requestCode when onActivityResult gets called on the given activity
	 * @return
	 * 		The dialog (not yet showing) created according to the given parameters
	 */
	public static Dialog makeDialog(final String title, final String message, 
									final String settingsButtonText, 
									final boolean cancelable, final OnCancelListener onCancel,
									final boolean skipable,
									final String skipButtonText, DialogInterface.OnClickListener onSkipClicked,
									final Activity activity, final int newActivityId)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(activity);
		builder.setTitle(title)
		   	   .setMessage(message)
		   	   .setPositiveButton(settingsButtonText,getSettingsButtonListner(activity,newActivityId))
		   	   .setCancelable(cancelable);
		if(cancelable && onCancel != null)
		{
			builder.setOnCancelListener(onCancel);
		}
		if(skipable)
		{
			builder.setNegativeButton(skipButtonText, onSkipClicked);
		}
    	return builder.create();
	}
	
	/**
	 * Makes the listener to be used by the settings button to allow 
	 * @param activity
	 * 		The activity to use to launch the location settings activity. This is the activity that
	 * 		onActivityResult will be called on when the user returns from the location settings
	 * @param newActivityId
	 * 		The id to be used as the requestCode when starting the location settings activity. This
	 * 		will be the value of requestCode when onActivityResult gets called on the given activity
	 * @return
	 * 		The listener to give to the settings button
	 */
	private static DialogInterface.OnClickListener getSettingsButtonListner(final Activity activity, final int newActivityId)
	{
		return new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
				//navigate to the location settings activity
				activity.startActivityForResult(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS), newActivityId);
			}
		};
	}
	
	
}
